﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities;
using Reviewer.Business;
using Reviewer.Entities.Services;

public partial class CMSV2_Controls_News_NewsRelation : System.Web.UI.UserControl
{
    public Guid NewsId
    {
        get
        {
            if (!String.IsNullOrEmpty(Request["NewsId"]))
            {
                return new Guid(Request["NewsId"]);
            }
            return Guid.Empty;
        }
    }

    private Guid[] Relations
    {
        get
        {
            if (ViewState["NewsGameRelations"] == null)
            {
                using (INewsService service = ServiceFactory.NewsService)
                {
                    List<Guid> selectedGames = new List<Guid>();
                    foreach (NewsGameRelation relation in service.GetGameRelations(this.NewsId))
                    {
                        selectedGames.Add(relation.GameId);
                    }
                    this.Relations = selectedGames.ToArray();
                }
            }
            return ViewState["NewsGameRelations"] as Guid[];
        }
        set
        {
            ViewState["NewsGameRelations"] = value;
        }
    }

    public void Refresh()
    {
        this.uiRpRelations.CurrentId = this.NewsId;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.uiRpRelations.CurrentType = typeof(News);
        }
    }

    protected bool uiRpRelations_IsRelationSelected(Type type, int id, Guid guid)
    {
        if (type.Equals(typeof(Game)))
        {
            return this.Relations.FirstOrDefault(t => t.Equals(guid)) == null;
        }
        return false;
    }

    protected void uiRpRelations_RelationSelected(Type type, int id, Guid guid)
    {
        if (type.Equals(typeof(Game)))
        {
            List<Guid> items = this.Relations.ToList();
            if (!items.Contains(guid))
            {
                items.Add(guid);
                this.Relations = items.ToArray();
            }
        }
    }

    protected void uiRpRelations_RelationUnSelected(Type type, int id, Guid guid)
    {
        if (type.Equals(typeof(Game)))
        {
            List<Guid> items = this.Relations.ToList();
            items.RemoveAll(t => t.Equals(guid));
            this.Relations = items.ToArray();
        }
    }

    protected void uiBtnSave_Click(object sender, EventArgs e)
    {
        Guid[] selectedGames = this.Relations;
        List<NewsGameRelation> relations = new List<NewsGameRelation>();
        foreach (Guid gameId in selectedGames)
        {
            NewsGameRelation relation = new NewsGameRelation();
            relation.GameId = gameId;
            relation.NewsId = this.NewsId;
            relations.Add(relation);
        }

        using (INewsAdminService adminService = ServiceFactory.NewsAdminService)
        {
            adminService.SetGameRelations(this.NewsId, relations.ToArray());
        }
    }
}
